package com.ygqh.baby.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;

/**
 * 签名工具类
 *
 * @className MapUtil
 * @see
 */
public class MapUtil {
	/**
	 * 连接字符串
	 *
	 * @param object
	 *            对象
	 * @param encoder
	 *            是否编码
	 * @param lowerCase
	 *            是否转换小写
	 * @param extra
	 *            附加对象
	 * @return
	 */
	public static String toJoinString(Object object, boolean encoder, boolean lowerCase) {
		Map<String, String> map = new HashMap<String, String>();
		JSONObject obj = null;
		if (object instanceof String) {
			obj = JSONObject.parseObject((String) object);
		} else {
			obj = ListsuffixResultSerializer.serializeToJSON(object);
		}
		for (String key : obj.keySet()) {
			map.put(key, obj.getString(key));
		}
		return toJoinString(map, encoder, lowerCase);
	}

	public static String toJoinStr(Map<String, String> map, boolean encoder, boolean lowerCase) {
		map = new TreeMap<String, String>(map);
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<String, String>> set = map.entrySet();
		try {
			if (encoder && lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append("&");
				}
			} else if (encoder) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append("&");
				}
			} else if (lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append("=").append(entry.getValue()).append("&");
				}
			} else {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
				}
			}
		} catch (UnsupportedEncodingException e) {
			;
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 拼接字符串
	 *
	 * @param map
	 *            对象
	 * @param encoder
	 *            是否编码
	 * @param lowerCase
	 *            是否转换小写
	 * @return
	 */
	public static String toJoinString(Map<String, String> map, boolean encoder, boolean lowerCase) {
		map.remove("sign");
		map = new TreeMap<String, String>(map);
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<String, String>> set = map.entrySet();
		try {
			if (encoder && lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append("&");
				}
			} else if (encoder) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append("&");
				}
			} else if (lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append("=").append(entry.getValue()).append("&");
				}
			} else {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
				}
			}
		} catch (UnsupportedEncodingException e) {
			;
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 拼接字符串
	 *
	 * @param map
	 *            对象
	 * @param encoder
	 *            是否编码
	 * @param lowerCase
	 *            是否转换小写
	 * @return
	 */
	public static String toJoinStrHasSize(Map<String, String> map, boolean encoder, boolean lowerCase) {
		map.remove("sign");
		map = new TreeMap<String, String>(map);
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<String, String>> set = map.entrySet();
		try {
			if (encoder && lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey().toLowerCase()).append(":")
							.append(getSize(entry.getKey(), "value")).append("-").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(";");
				}
			} else if (encoder) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey()).append(":").append(getSize(entry.getValue(), "value"))
							.append("-").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(";");
				}
			} else if (lowerCase) {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey().toLowerCase()).append(":")
							.append(getSize(entry.getValue(), "value")).append("-").append(entry.getValue()).append(";");
				}
			} else {
				for (Map.Entry<String, String> entry : set) {
					if (StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey()).append(":").append(getSize(entry.getValue(), "value"))
							.append("-").append(entry.getValue()).append(";");
				}
			}
		} catch (UnsupportedEncodingException e) {
			;
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	private static String getSize(String str, String type) {
		String result = str.length() + "";
		if (type.equals("key")) {
			if (str.length() < 10) {
				result = "0" + str.length();
			}
		} else {
			if (str.length() < 10) {
				result = "000" + str.length();
			} else if (str.length() >= 10 && str.length() < 100) {
				result = "00" + str.length();
			} else if (str.length() >= 100 && str.length() < 1000) {
				result = "0" + str.length();
			}
		}

		return result;

	}

	/**
	 * 拼接字符串
	 *
	 * @param map
	 *            对象
	 * @param encoder
	 *            是否编码
	 * @param lowerCase
	 *            是否转换小写
	 * @return
	 */
	public static String toJoinStrASCII(Map<String, String> map, boolean encoder, boolean lowerCase) {
		map.remove("app_sign");
		Map<String, String> tmpMap = map;
		List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(tmpMap.entrySet());
		Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
			@Override
			public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
				return (o1.getKey()).toString().compareTo(o2.getKey());
			}
		});
		StringBuilder sb = new StringBuilder();
		try {
			if (encoder && lowerCase) {
				for (Map.Entry<String, String> entry : infoIds) {
					if (StringUtil.isBlank(entry.getKey()) || StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name()));
				}
			} else if (encoder) {
				for (Map.Entry<String, String> entry : infoIds) {
					if (StringUtil.isBlank(entry.getKey()) || StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name()));
				}
			} else if (lowerCase) {
				for (Map.Entry<String, String> entry : infoIds) {
					if (StringUtil.isBlank(entry.getKey()) || StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey().toLowerCase()).append(entry.getValue());
				}
			} else {
				for (Map.Entry<String, String> entry : infoIds) {
					if (StringUtil.isBlank(entry.getKey()) || StringUtil.isBlank(entry.getValue())) {
						continue;
					}
					sb.append(entry.getKey()).append(entry.getValue());
				}
			}
		} catch (UnsupportedEncodingException e) {
			;
		}
		return sb.toString();
	}
}
